package com.cn.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.persistence.criteria.Path;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;

import org.apache.struts2.ServletActionContext;

import com.alipay.api.domain.StudentInfo;
import com.cn.domain.Book;
import com.cn.domain.Category;
import com.cn.service.BookService;
import com.cn.service.CategoryService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class BookAction extends ActionSupport implements ModelDriven<Book> {

	private BookService bookService;
	private CategoryService categoryService;

	private Integer choiceId;
	/*
	 * 封装文件上传的表单参数
	 */

	private File upload;// 实际上传的文件
	// 封装上传文件类型
	private String uploadContentTypeString;

	private String uploadFileName;// 上传文件的名字

	Book model = new Book();

	@Override
	public Book getModel() {
		return model;
	}

	public Integer getChoiceId() {
		return choiceId;
	}

	public void setChoiceId(Integer choiceId) {
		this.choiceId = choiceId;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentTypeString() {
		return uploadContentTypeString;
	}

	public void setUploadContentTypeString(String uploadContentTypeString) {
		this.uploadContentTypeString = uploadContentTypeString;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	// 查询所有图书
	public String findAllBook() {
		List<Category> listcategory = categoryService.findAllCategory();
		ActionContext.getContext().getValueStack()
				.set("listcategory", listcategory);
		List<Book> listbook = bookService.findAllBook();
		ActionContext.getContext().getValueStack().set("listbook", listbook);
		return "bookall";
	}

	/*
	 * 详细查询图书
	 */
	public String introPage() {
		Book book = bookService.getBookId(model.getBid());
		ActionContext.getContext().getValueStack().set("book", book);
		List<Category> listcategory = categoryService.findAllCategory();
		ActionContext.getContext().getValueStack()
				.set("listcategory", listcategory);
		return "intropage";
	}

	/*
	 * 只查询所有的图书
	 */
	public String findAdminAllBook() {
		List<Book> listbook = bookService.findAllBook();
		ActionContext.getContext().getValueStack().set("listbook", listbook);
		return "bookadminall";
	}

	/*
	 * 修改书籍
	 */
	public String editBookPage() {
		// 数据回显
		Book book = bookService.getBookId(model.getBid());
		ActionContext.getContext().getValueStack().set("book", book);
		return "editbookpage";

	}

	/*
	 * 修改书籍的逻辑代码
	 */
	public String editBook() throws Exception {

		InputStream in = null;
		java.io.OutputStream out = null;
		String path = null;

		try {
			in = new FileInputStream(this.upload);
			path = ServletActionContext.getServletContext()
					.getRealPath("/file");
			out = new FileOutputStream(path + File.separator
					+ this.uploadFileName);
			System.out.println("file" + File.separator + this.uploadFileName);
			byte[] buf = new byte[2048];
			int len = 0;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		}
		System.out.println("pircture=" + model.getPicture());
		Book book = bookService.getBookId(model.getBid());
		book.setBname(model.getBname());
		book.setAuthor(model.getAuthor());
		book.setQuantity(model.getQuantity());
		book.setPrice(model.getPrice());
		book.setPress(model.getPress());
		// model.setPicture(model.getPicture());
		book.setPicture("file" + File.separator + this.uploadFileName);
		book.setIntro(model.getIntro());
		bookService.updateBook(book);
		// book.setPicture(picture);
		return "editbook";
	}

	/*
	 * 删除书籍
	 */
	public String deleteBook() {

		Book book = bookService.getBookId(model.getBid());
		bookService.deleteBook(book);
		return "deletebook";
	}

	/*
	 * 添加图书
	 */
	public String addBookPage() {
		List<Category> cList = categoryService.findAllCategory();
		ActionContext.getContext().getValueStack().set("cList", cList);

		return "addbookpage";
	}

	/*
	 * 添加图书的逻辑代码
	 */
	public String addBook() throws Exception {

		InputStream in = null;
		java.io.OutputStream out = null;
		String path = null;

		try {
			in = new FileInputStream(this.upload);
			path = ServletActionContext.getServletContext()
					.getRealPath("/file");
			out = new FileOutputStream(path + File.separator
					+ this.uploadFileName);
			System.out.println("file" + File.separator + this.uploadFileName);
			byte[] buf = new byte[2048];
			int len = 0;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		}
		model.setIs_hot("1");
		model.setPicture("file" + File.separator + this.uploadFileName);
		System.out.println("id=" + choiceId);// 获取下拉菜单的选中项
		model.setCategory(categoryService.getCategoryId(choiceId));
		bookService.saveBook(model);

		return "addbook";
	}

}
